/**
 * 
 */
package com.jie.eclipse.pan.swtitem.actiongroup;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.viewers.CheckboxTableViewer;

import com.jie.eclipse.pan.config.BaseConfig;
import com.jie.eclipse.pan.data.BaseDataEntities;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.swtitem.table.PTableViewer;
import com.jie.eclipse.pan.swtitem.widgets.BaseComposite;
import com.jie.eclipse.pan.utils.Utils;
import com.jie.eclipse.pan.utils.handle.FileHandle;

/**
 * @author yanjie
 *
 */
public class SelectedColumnsActionGroup extends BaseActionGroup{
	/**
	 * @param bc
	 * @param fileHandle
	 */
	public SelectedColumnsActionGroup(BaseComposite bc, FileHandle fileHandle) {
		super(bc, fileHandle);
	}
	
	private class AddRowAction extends Action{
		public AddRowAction() {
			this.setImageDescriptor(getImageDesc("icon_addrow.gif"));
			this.setText("新增行");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 = (CheckboxTableViewer)getBaseComposite().getDataInstance().gpov("ctv1");//可选栏位表格选择器
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getBaseComposite().getDataInstance().gpov("ctv2");//已选栏位表格选择器
			PTableViewer tv2 = (PTableViewer)getBaseComposite().getDataInstance().gpov("tv2");//已选栏位表格选择器
			if(ctv1 == null || ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			BaseDataEntity row = new BaseDataEntity();
			row.spsv("id", tv2.getNextId());
			row.spsv("cid", tv2.getNextId());
			row.spsv("width", "");
			row.spsv("caption", "新增列");
			ctv2.add(row);
		}
	}
	
	private class DeleteRowAction extends Action{
		public DeleteRowAction() {
			this.setImageDescriptor(getImageDesc("icon_delete.gif"));
			this.setText("删除行");
		}
		
		@Override
		public void run() {
			CheckboxTableViewer ctv1 = (CheckboxTableViewer)getBaseComposite().getDataInstance().gpov("ctv1");//可选栏位表格选择器
			CheckboxTableViewer ctv2 = (CheckboxTableViewer)getBaseComposite().getDataInstance().gpov("ctv2");//已选栏位表格选择器
			if(ctv1 == null || ctv2 == null){
				Utils.print("表格选择器不正常");
				return;
			}
			getFileHandle().deleteTableRows(ctv2.getCheckedElements());
			ctv2.remove(ctv2.getCheckedElements());
		}
	}
	
	public void fillActionToolbars (ToolBarManager actionBarManager){
		AddRowAction addrowAction = new AddRowAction();
		DeleteRowAction deleterowAction = new DeleteRowAction();
		
		actionBarManager.add(createActionContributionItem(addrowAction));
		actionBarManager.add(createActionContributionItem(deleterowAction));
		actionBarManager.update(true);
	}
}
